<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input size="mini"  clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" >搜索</el-button>
      <!-- 新增 -->
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
        <el-button
          type="danger"
          class="filter-item"
          size="mini"
          icon="el-icon-refresh"
          @click="getList"
        >刷新</el-button>
      </div>
    </div>
    <e-form :isAdd="isAdd" ref="form" @refresh="getList"></e-form>
    <!--表格渲染-->
    <el-table :data="list" size="small" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="locationId" label="地点id" />
      <el-table-column prop="locationName" label="医院名称" />
      <el-table-column prop="province" label="省份" />
      <el-table-column prop="city" label="城市" />
      <el-table-column prop="locationAddr" label="详细地址" />
      <el-table-column prop="longitude" label="经度" />
      <el-table-column prop="latitude" label="纬度" />
      <el-table-column prop="pointNum" label="坐标点个数" />
      <el-table-column label="操作" width="205px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
          <span style="display: inline-block;width: 3px;"></span>
          <el-popover
            :ref="scope.row.locationId"
            placement="top"
            width="180"
          >
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.locationId].doClose()">取消</el-button>
              <el-button  :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.locationId)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import siteApi from '@/network/map/site.api'

import EForm from './form'
import {mapActions} from 'vuex'
export default {
  name: 'Site',
  components: {
    EForm,
  },
  data() {
    return {
     total: 10,
     page: 1,
     size: 10,
     isDialog: false,
     loading: false,
     delLoading: false,
     isAdd: true,
     list: [
     ]
    }
  },
  created() {
   this.getList();
  },
  methods: {
    ...mapActions('hospital',['saveHospitals']),
    async getList() {
      const {page, size} = this
      let res = await siteApi.getSiteList(page,size)
      this.list = res.locationList
      this.total = res.Count
    },
    sizeChange(size) {
      this.size = size
      this.getList()
    },
    pageChange(page) {
      this.page = page
      this.getList()
    },
    subDelete(id) {
      this.delLoading = true
      siteApi.delSite(id).then(() => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.getList();
        this.saveHospitals()
         this.$message({message: '删除成功', type: 'success',duration: 2500})
      }).catch(() => {
        this.delLoading = false
        // this.$refs[id].doClose()
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.isDialog = true
    },
    edit(data) {
      this.isAdd = false
      this.$refs.form.isDialog = true
      this.$refs.form.ruleForm = {...data}
      this.$refs.form.selectProvince(data.province,data.city)
    },
  }
}
</script>

<style lang="scss" scoped>
.location-id {
  color:  #409EFF;
  cursor: pointer;
}
.head-container .filter-item {
      display: inline-block;
      vertical-align: middle;
    margin: 0 2px 10px 0;
}
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
    border: 0.5px dashed #8c939d;
    border-radius: 5px;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>